package com.yuyou.tas.lesson.controller;

import com.yuyou.tas.lesson.service.ICommDataConfigService;
import com.yuyou.tas.service.api.admin.entity.CommDataConfig;
import com.yuyou.tas.util.bean.LoginRedisObj;
import com.yuyou.tas.util.bean.ReturnResult;
import com.yuyou.tas.util.enums.admin.AuthUserEnums;
import com.yuyou.tas.util.enums.admin.CommDataConfigEnums;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(value="BaseDataController",description="数据字典接口")
@Validated
@RestController
@RequestMapping(value="/api/baseDataController")
public class BaseDataController extends BaseController{

	@Autowired
	private ICommDataConfigService commDataConfigService;
	
	
	@ApiOperation(value="获取年份")
	@RequestMapping(value="/findYearsData",method = {RequestMethod.GET, RequestMethod.POST})
	public ReturnResult<List<CommDataConfig>> findYearsData(){
        List<CommDataConfig> list=commDataConfigService.findUseByCategory(CommDataConfigEnums.category.year.getValue());
		return ReturnResult.success(list);
	}


    @ApiOperation(value="获取科目")
    @RequestMapping(value="/findSubjectsData",method = {RequestMethod.GET, RequestMethod.POST})
    public ReturnResult<List<CommDataConfig>> findSubjectsData(HttpServletRequest request,
                                                               @ApiParam(value = "年级ID")@RequestParam(value = "sectionId",required = false) String sectionId,
                                                               @ApiParam(value = "是否通过授课班级获取：1、是，0或不传、否")@RequestParam(value = "tag",required = false) Integer tag){
        LoginRedisObj user = this.getUserObj(request);

        if(tag != null && tag == 1) {//非管理员-科目权限
            return ReturnResult.success(commDataConfigService.findUserSubject(user.getUserId() , StringUtils.isEmpty(sectionId)?null:Long.valueOf(sectionId)));
        }else if(!user.getIsManager() && user.getUserType().equals(AuthUserEnums.type.teaching.getValue())){//教研
            return ReturnResult.success(commDataConfigService.findTeachingSubject(user.getUserId() , StringUtils.isEmpty(sectionId)?null:Long.valueOf(sectionId)));
        }

        Map<String , Object> condition = new HashMap<>();
        condition.put("category",CommDataConfigEnums.category.subject.getValue());



        if(!StringUtils.isEmpty(sectionId)){
            condition.put("sectionID",sectionId.trim());
        }
        List<CommDataConfig> list=commDataConfigService.findSubject(condition);
        return ReturnResult.success(list);
    }


    @ApiOperation(value="获取教材版本")
    @RequestMapping(value="/findMaterialData",method = {RequestMethod.GET, RequestMethod.POST})
    public ReturnResult<List<CommDataConfig>> findMaterialData(HttpServletRequest request,
                                                               @ApiParam(value = "年级ID")@RequestParam(value = "sectionId",required = false) String sectionId,
                                                               @ApiParam(value = "科目ID")@RequestParam(value = "subjectId",required = false) String subjectId){
        List<CommDataConfig> list;
        if(sectionId == null && subjectId == null){
            list=commDataConfigService.findUseByCategory(CommDataConfigEnums.category.material.getValue());
        }else{
            Map<String, Object> conditions = new HashMap<>();
            LoginRedisObj user = this.getUserObj(request);
            conditions.put("organ_id", user.getOrganizationId());
            if(!StringUtils.isEmpty(subjectId)){
                conditions.put("subjectId", subjectId.trim());
            }
            if(!StringUtils.isEmpty(sectionId)){
                conditions.put("sectionID", sectionId.trim());
            }
            list = commDataConfigService.findMaterial(conditions);
        }

        return ReturnResult.success(list);
    }


    @ApiOperation(value="获取期类型")
    @RequestMapping(value="/findTimeTypeData",method = {RequestMethod.GET, RequestMethod.POST})
    public ReturnResult<List<CommDataConfig>> findTypeData(){
        List<CommDataConfig> list=commDataConfigService.findUseByCategory(CommDataConfigEnums.category.timeType.getValue());
        return ReturnResult.success(list);
    }

    @ApiOperation(value="获取题目类型")
    @RequestMapping(value="/findQuestionTypeData",method = {RequestMethod.GET, RequestMethod.POST})
    public ReturnResult<List<CommDataConfig>> findQuestionTypeData(){
        List<CommDataConfig> list=commDataConfigService.findUseByCategory(CommDataConfigEnums.category.questionType.getValue());
        return ReturnResult.success(list);
    }

}
